// pages/user/invoiceList/invoiceList.js
var app = getApp();
var pageSize = 7;//默认页量
var page_s = 1;//默认页码
Page({

  /**
   * 页面的初始数据
   */
  data: {
    invoiceList: [],  // 发票列表
    is_load: false,
    history_List:[],//开票记录
    hasMore: false, //数据是否加载完成
    loading: false,
    load_show:false,//是否加载
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getInvoiceList();  // 发票列表
    that.setData({
      history_List: [],
      load_show: false
    })
   that.getInvoiceHistoryList(1);//获取开票记录
  },

  // 获取发票列表
  getInvoiceList () {
    var that = this;
    // ajax请求
    app.ajax({
      url: 'User/Invoice/invoiceList',
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            invoiceList: res.data.data   // 数据赋值
          })
        }
        that.setData({
          is_load:true
        })
      }
    })
  },

  // 点击发票跳转并传值
  editInvoice(e) {
    var ty_pes = e.currentTarget.dataset.types;   // 发票类型
    var invoice_id = e.currentTarget.dataset.id;   // 标识符
    wx.navigateTo({
      url: '../editInvoice/editInvoice?types=' + ty_pes + '&invoice_id=' + invoice_id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    // 当没有数据时,不再请求
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    page_s += 1;
    that.getInvoiceHistoryList(page_s);
  },
  //invoiceDetail  跳转详情
  goDetail(e){
    var id = e.currentTarget.dataset.id;   // 发票id
    wx.navigateTo({
      url: '../invoiceDetail/invoiceDetail?id=' + id,
    })
  },
  //获取发票列表
  getInvoiceHistoryList(page_s){
    var that = this;
    app.ajax({
      url: 'User/Invoice/getRecord',
      method: "POST",
      data: {
        page: page_s,
        pageSize: pageSize },
      success: function (res) {
        if (res.data.code == 1000) {
          //是否需要加载更多
          var hasMore = true;
          if (res.data.data.length < pageSize) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          })
          var changeList = "";
          if (that.data.history_List == "") {
            changeList = res.data.data;
          } else {
            var addList = that.data.history_List;
            changeList = addList.concat(res.data.data);
          }
          that.setData({
            history_List: changeList,
          })
        } else {
          // app.showToast(res.data.message, "none", 2000, function () { });
        }
        that.setData({
          load_show: true,
        })
      }

    })
  }
})